package com.weishi.project.api.controller;

import com.weishi.common.utils.DateUtils;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.training.domain.*;
import com.weishi.project.training.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/app/synchronizeData")
@Api(value = "SynchronizeData", description = "同步数据")
public class ApiSynchronizeData  extends BaseController {

    @Autowired
    private INaturalYearService naturalYearService;

    @Autowired
    private IAnnualPlanService annualPlanService;

    @Autowired
    private IAnnualProjectService annualProjectService;

    @Autowired
    private ITrainingWeekService trainingWeekService;

    @Autowired
    private ITrainingGroupService trainingGroupService;

    @Autowired
    private ITrainingGroupChannelService trainingGroupChannelService;

    @Autowired
    private ITrainingDayService trainingDayService;

    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    @ApiOperation(value = "按天同步")
    @GetMapping("/synchronizeDay")
    @Transactional
    public AjaxResult synchronizeDay(@RequestParam("day") String day,
                                     @RequestParam("toDay") String toDay) {
        List<NaturalYear> list = naturalYearService.selectNaturalYearList(new NaturalYear());
        Date date = DateUtils.parseDate(toDay);
        TrainingWeek trainingWeek = trainingWeekService.selectTrainingWeekByDay(DateUtils.parseDate(toDay));

        TrainingDay trainingDayTemp = new TrainingDay();
        trainingDayTemp.setDay(day);
        List<TrainingDay> trainingDays= trainingDayService.selectTrainingDayList(trainingDayTemp);
        for(TrainingDay trainingDay : trainingDays){
            if(trainingDay.getId()==null){
                continue;
            }
            TrainingDay trainingDayVo = new TrainingDay();
            BeanUtils.copyProperties(trainingDay,trainingDayVo);
            trainingDayVo.setId(null);
            trainingDayVo.setDay(toDay);
            trainingDayVo.setWeekId(trainingWeek.getId());
            trainingDayVo.setCtime(date);
            trainingDayService.save(trainingDayVo);

            TrainingGroup trainingGroupTemp = new TrainingGroup();
            trainingGroupTemp.setTrainingDayId(trainingDay.getId());
            List<TrainingGroup> trainingGroups = trainingGroupService.selectTrainingGroupList1(trainingGroupTemp);
            for(TrainingGroup trainingGroup : trainingGroups){
                if(trainingGroup.getId()==null){
                    continue;
                }
                BeanUtils.copyProperties(trainingGroup,trainingGroupTemp);
                trainingGroupTemp.setId(null);
                trainingGroupTemp.setTrainingDayId(trainingDayVo.getId());
                trainingGroupTemp.setWeekId(trainingWeek.getId());
                trainingGroupTemp.setCtime(date);
                trainingGroupService.save(trainingGroupTemp);

                TrainingGroupChannel trainingGroupChannelTemp = new TrainingGroupChannel();
                trainingGroupChannelTemp.setGroupId(trainingGroup.getId());
                List<TrainingGroupChannel> trainingGroupChannels = trainingGroupChannelService.getTrainingGroupChannelByGroupId(trainingGroup.getId(),false);
                if(trainingGroupChannels==null){
                    continue;
                }
                for(TrainingGroupChannel trainingGroupChannel : trainingGroupChannels){
                    if(trainingGroupChannel.getId()==null){
                        continue;
                    }
                    BeanUtils.copyProperties(trainingGroupChannel,trainingGroupChannelTemp);
                    trainingGroupChannelTemp.setId(null);
                    trainingGroupChannelTemp.setGroupId(trainingGroupTemp.getId());
                    trainingGroupChannelTemp.setCtime(date);
                    trainingGroupChannelService.save(trainingGroupChannelTemp);

                    TrainingAchievement trainingAchievementTemp = new TrainingAchievement();
                    trainingAchievementTemp.setChannelId(trainingGroupChannel.getId());
                    List<TrainingAchievement> trainingAchievements = trainingAchievementService.selectTrainingAchievementList1(trainingAchievementTemp);
                    for(TrainingAchievement trainingAchievement : trainingAchievements){
                        if(trainingAchievement.getId()==null){
                            continue;
                        }
                        BeanUtils.copyProperties(trainingAchievement,trainingAchievementTemp);
                        trainingAchievementTemp.setId(null);
                        trainingAchievementTemp.setWeekId(trainingWeek.getId());
                        trainingAchievementTemp.setTrainingDayId(trainingDayVo.getId());
                        trainingAchievementTemp.setGroupId(trainingGroupTemp.getId());
                        trainingAchievementTemp.setChannelId(trainingGroupChannelTemp.getId());
                        trainingAchievementTemp.setCtime(date);
                        trainingAchievementService.save(trainingAchievementTemp);
                    }
                }
            }

        }
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "按周同步")
    @GetMapping("/synchronizeWeek")
    public AjaxResult synchronizeWeek(@RequestParam("week") String week,
                                      @RequestParam("toWeek") String toWeek) {
        List<NaturalYear> list = naturalYearService.selectNaturalYearList(new NaturalYear());
        return AjaxResult.success(list);
    }
}
